package com.example.common_library.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.example.common_library.R;

/**
 * @desc titlebar
 */
public class TittleBarView extends RelativeLayout {
    private RelativeLayout rlRoot;
    private TextView tvTitle;
    private ImageView ivGoBack;
    private Context context;
    private String title;
    private int backImg, bgColor;

    private GoBackClickListener goBackListener;


    public TittleBarView(Context context) {
        super(context);
    }

    public TittleBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TittleBarView);
        title = typedArray.getString(R.styleable.TittleBarView_title);
        backImg = typedArray.getResourceId(R.styleable.TittleBarView_backImg, R.mipmap.ic_back_white);
        bgColor = typedArray.getColor(R.styleable.TittleBarView_bgColor, Color.parseColor("#121212"));
        typedArray.recycle();
        init();
    }

    public TittleBarView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void init() {
        /**
         * 将自定义控件和布局文件进行绑定
         * 参数一:View中可以通过getContext方法获取上下文对象
         * 参数二:布局文件id
         * 参数三:放置该布局的容器,也就是本类
         */
        View view = View.inflate(getContext(), R.layout.layout_titlebar, this);
        rlRoot = (RelativeLayout) view.findViewById(R.id.rl_root);
        ivGoBack = (ImageView) view.findViewById(R.id.iv_back);
        tvTitle = (TextView) view.findViewById(R.id.tv_title);

        rlRoot.setBackgroundColor(bgColor);
        ivGoBack.setImageResource(backImg);
        tvTitle.setText(title);
        ivGoBack.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (goBackListener == null) return;
                goBackListener.onClick(v);
            }
        });

    }

    /**
     * 设置标题栏颜色
     */
    public TittleBarView setTitleBackground(int color) {
        rlRoot.setBackgroundColor(ContextCompat.getColor(context, color));
        return this;
    }

    /**
     * 设置标题
     *
     * @param title
     */
    public TittleBarView setTitle(String title) {
        tvTitle.setText(title);
        return this;
    }

    /**
     * 设置标题颜色
     *
     * @param color
     */
    public void setTitleColor(int color) {
        tvTitle.setTextColor(color);
    }

    /**
     * 设置左方图片的资源文件
     *
     * @param resId
     */
    public void setLeftImageView(int resId) {
        ivGoBack.setImageResource(resId);
    }


    public void setTitleTextSize(int size) {
        tvTitle.setTextSize(size);
    }


    /**
     * 返回按钮点击的接口
     */
    public interface GoBackClickListener {
        void onClick(View view);
    }


    public void setGoBackListener(GoBackClickListener listener) {
        this.goBackListener = listener;
    }


}
